package tgc.edu.fhh.web.controller;


import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.fhh.custom.AjaxResult;
import tgc.edu.fhh.custom.JwtTokenUtils;
import tgc.edu.fhh.entity.SysUser;
import tgc.edu.fhh.sercurity.UserUtils;
import tgc.edu.fhh.service.SysUserService;

@RestController
public class MainController {
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private SysUserService service;
	@RequestMapping(value = "/loginSucc")
	public Object loginSucc() {
		String name = userUtils.getName();
		String username = userUtils.getUsername();
		String roles = userUtils.getRoles();
		String token = JwtTokenUtils.createToken(username, name, roles);
		token=JwtTokenUtils.TOKEN_PREFIX+token;  //加上token前缀
	    HashMap<String, Object> result=new HashMap<String, Object>();
	    result.put("token",token);
	    result.put("name", name);
	    return new AjaxResult("登录成功", result);
	}
	
	@RequestMapping(value = "/loginFail")
	public Object loginFail() {
		return new AjaxResult(false,"用户名或密码错误，登录失败");
	}
	
	@RequestMapping(value = "/logoutSucc")
	public Object logoutSucc() {
		return new AjaxResult("注销成功！");
	}
	
	@RequestMapping(value = "/uppwd")
	public Object uppwd(String npas) {
		SysUser user = userUtils.getUser();
		String xpwd = npas;
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		String password = encoder.encode(xpwd);
		user.setPassword(password);
		service.save(user);
		return new AjaxResult("数据修改成功");
	}

}
